//
//  WFDrop_DownView.m
//  谷居
//
//  Created by dllo on 16/6/24.
//  Copyright © 2016年 刘万方. All rights reserved.
//

#import "WFDrop_DownView.h"
#import "WFDropCollectionViewCell.h"
#import "WFCaseTitleModel.h"
#import "WFPicsTitleModel.h"
#import "WFGuideTitleModel.h"

@interface WFDrop_DownView () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, assign) NSInteger navigationType;
@end

@implementation WFDrop_DownView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
//        [self createCollectionView];
    }
    return self;
}


- (void)createCollectionViewWithArray:(NSMutableArray *)array andNavigationType:(NSInteger)type index:(NSInteger)index{
    self.index = index;
    /** 传入的navigationtype的类型. */
    self.navigationType = type;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(90, 40);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = (SCREEN_WIDTH - 270) / 4;
    layout.sectionInset = UIEdgeInsetsMake(10, (SCREEN_WIDTH - 270) / 4, 10, (SCREEN_WIDTH - 270) / 4);
    
    /** 设置自适应高度. */
    /** 设置行数. */
    
    CGFloat spcaceNUM = array.count  / 3 + (array.count % 3 > 0 ? 1 : 0);
    CGFloat height = spcaceNUM * 40 + (spcaceNUM - 1) * 20 + 20;
    _collectionView = [[WFCollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height) collectionViewLayout:layout];
    [self addSubview:_collectionView];
    [_collectionView release];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.scrollEnabled = NO;
    [_collectionView registerClass:[WFDropCollectionViewCell class] forCellWithReuseIdentifier:@"drop_class"];
    
    /** 将collectionview的数组传值. */
    self.titleArray = array;
}




- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.titleArray.count;
}





- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WFDropCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"drop_class" forIndexPath:indexPath];
    /** 判断是哪个界面传来的. */
    switch (self.navigationType) {
        case 0:
        {
            WFCaseTitleModel *model = self.titleArray[indexPath.item];
            cell.titleLabel.text = model.name;
        }
            break;
        case 1:
        {
            WFPicsTitleModel *model = self.titleArray[indexPath.item];
            cell.titleLabel.text = model.name;
            if (self.index == 2) {
//                cell.titleLabel.backgroundColor = [UIColor colorWithCIColor:<#(nonnull CIColor *)#>
            }
        }
            break;
        case 2:
        {
            WFGuideTitleModel *model = self.titleArray[indexPath.item];
            cell.titleLabel.text = model.name;
        }
            break;
            
            
        default:
            break;
    }
    
    
    cell.titleLabel.textAlignment = 1;
    cell.titleLabel.font = [UIFont systemFontOfSize:15];
    cell.titleLabel.layer.borderWidth = 1;
    cell.titleLabel.layer.borderColor = [UIColor grayColor].CGColor;
    cell.backgroundColor = [UIColor lightGrayColor];
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    /** 当点击item之后向WENavigationView中传一个值. 将值传到caseView中刷新数据. */
        switch (self.navigationType) {
        case 0:
        {
            WFCaseTitleModel *model = self.titleArray[indexPath.item];
            [self.delegate takeTitleButtonName:model.name andId:model.id];

        }
            break;
        case 1:
        {
            WFPicsTitleModel *model = self.titleArray[indexPath.item];
            [self.delegate takeTitleButtonName:model.name andId:[NSString stringWithFormat:@"%@", model.id]];
        }
            break;
        case 2:
        {
            WFGuideTitleModel *model = self.titleArray[indexPath.item];
            [self.delegate takeTitleButtonName:model.name andId:[NSString stringWithFormat:@"%ld", model.id]];
        }
            break;
            
            
        default:
            break;
    }
    

}
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
//    [self.delegate clickDrop_downView];

}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    [self.delegate clickDrop_downView];
}


- (void)removeCollectionView {
    [_collectionView removeFromSuperview];
}
- (void)dealloc {
    
    
//    [_caseTitleArray release];
    [super dealloc];
}
@end
